package com.jerry.huihome.module.home.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.home.adapter.HomeBannerAdapter;
import com.jerry.huihome.module.home.adapter.HomeBottomActivityAdapter;
import com.jerry.huihome.module.home.adapter.HomeBottomAdapter;
import com.jerry.huihome.module.home.adapter.PageAdapterListener;
import com.jerry.huihome.module.home.entity.DrawHouse;
import com.jerry.huihome.utils.EventMessage;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class DrawHomeFragment extends BaseFragment{
    private List<DrawHouse.BodyBean.BannersBean> bannersBeanList = new ArrayList<>();
    private List<DrawHouse.BodyBean.PicturesBean> picturesList = new ArrayList<>();
    private List<DrawHouse.BodyBean.ActivitiesBean> activitiesBeanList = new ArrayList<>();
    private List<DrawHouse.BodyBean.ProjectsBean> projectsBeenList = new ArrayList<>();

    @BindView(R.id.vp_ad_banner)
    public ViewPager vpADSBanner;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.rg_banner)
    public RadioGroup rgBanner;

    @BindView(R.id.lv_home_afflatus)
    public ListView lvListView;

    @BindView(R.id.lv_home_construction)
    public ListView lvConstruction;

    @BindView(R.id.lv_home_activity)
    public ListView lvActivity;

    private HomeBannerAdapter mBannerAdapter;
    private HomeBottomAdapter mBottomJAdapter;
    private HomeBottomAdapter mBottomPAdapter;
    private HomeBottomActivityAdapter lvActivityAdapter;
    //    private final FragmentManager manager = ((MainActivity)getActivity()).getSupportFragmentManager();

    public DrawHomeFragment(){
    }

    @Override
    protected void getReqParams(Bundle bundle){
        String tag = bundle.getString("tag");
    }

    @Override
    protected int getLayoutId(){
        return R.layout.frag_default_home;
    }

    @Override
    protected void initViews(){
        mBannerAdapter = new HomeBannerAdapter(getActivity(), bannersBeanList);
        vpADSBanner.setAdapter(mBannerAdapter);
        vpADSBanner.addOnPageChangeListener(new PageAdapterListener(){
            @Override
            public void onPageSelected(int position){
                RadioButton view = (RadioButton) rgBanner.getChildAt(position);
                view.setChecked(true);
                currentPageNO = position;
            }
        });
        mBottomPAdapter = new HomeBottomAdapter(getContext(), picturesList);
        lvListView.setAdapter(mBottomPAdapter);
        mBottomJAdapter = new HomeBottomAdapter(getContext(), projectsBeenList);
        lvConstruction.setAdapter(mBottomJAdapter);
        lvActivityAdapter = new HomeBottomActivityAdapter(getContext(), activitiesBeanList);
        lvActivity.setAdapter(lvActivityAdapter);
        scrollView.smoothScrollTo(10, 10);
        vpADSBanner.setCurrentItem(0);
    }

    int currentPageNO = 0;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            currentPageNO = ++currentPageNO % rgBanner.getChildCount();
            View view = rgBanner.getChildAt(currentPageNO);
            view.setSelected(true);
            vpADSBanner.setCurrentItem(currentPageNO);
            mHandler.sendEmptyMessageDelayed(1, 5000);
        }
    };

    @Override
    protected void loadData(){
        OkHttpUtils.get(Urls.DRAW_HOUSE_URL, new OkHttpUtils.OnGetDataListener(){
            @Override
            public void onResponse(String url, String data){
                if(url.equals(Urls.DRAW_HOUSE_URL)){
                    Gson gson = new Gson();
                    DrawHouse house = gson.fromJson(data, DrawHouse.class);
                    if(house != null){
                        bannersBeanList.addAll(house.getBody().getBanners());
                        picturesList.addAll(house.getBody().getPictures());
                        projectsBeenList.addAll(house.getBody().getProjects());
                        activitiesBeanList.addAll(house.getBody().getActivities());
                        timeChangeBanner();
                    }
                    mBannerAdapter.notifyDataSetChanged();
                    mBottomPAdapter.notifyDataSetChanged();
                    mBottomJAdapter.notifyDataSetChanged();
                    lvActivityAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(String url, String error){
            }
        });
    }

    @OnClick(R.id.rb_onlineHelp)
    public void getHelp(){
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_helpdialog, null);
        AlertDialog mDialog = new AlertDialog.Builder(getActivity()).create();
        mDialog.show();
        mDialog.getWindow().setContentView(view);
        TextView tvOnCall = (TextView) view.findViewById(R.id.tv_onCall);
        TextView tvOnline = (TextView) view.findViewById(R.id.tv_online);
        tvOnCall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:4000058285"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        tvOnline.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(getActivity(), "功能完善中……", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.rb_select_designer)
    public void selectDesigner(){
        EventBus.getDefault().post(new EventMessage("seek","seek"));
    }
    @OnClick({R.id.rb_find_inspiration,R.id.skip_find_inspiration})
    public void findInspiration(){
        EventBus.getDefault().post(new EventMessage("find","find"));
    }
    @OnClick({R.id.rb_activity,R.id.skip_activity})
    public void Activity(){
        EventBus.getDefault().post(new EventMessage("activity","activity"));
    }
    @OnClick(R.id.skip_live)
    public void skipPlace(){
        EventBus.getDefault().post(new EventMessage("place","place"));
    }

    private void timeChangeBanner(){
        mHandler.sendEmptyMessageDelayed(1, 5000);
    }
}
